package com.cetccloud.ap.vertx.verticle;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.core.http.HttpServer;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName MyVerticle
 * @Description TODO
 * @Author superz
 * @Date 2021/4/10 21:35
 * @Version 1.0
 **/
@Slf4j
public class MyVerticle extends AbstractVerticle {

    private HttpServer server;

    @Override
    public void start(Promise<Void> startPromise) {
        log.info("start promise, {}", config());
        server = vertx.createHttpServer().requestHandler(req -> {
            req.response()
                    .putHeader("content-type", "text/plain")
                    .end("Hello from Vert.x!");
        });

        // Now bind the server:
        server.listen(8080, res -> {
            if (res.succeeded()) {
                startPromise.complete();
            } else {
                startPromise.fail(res.cause());
            }
        });
    }

    @Override
    public void stop(Promise<Void> stopPromise) {
        log.info("stop promise");
        server.close(res -> {
            if (res.succeeded()) {
                stopPromise.complete();
            } else {
                stopPromise.fail(res.cause());
            }
        });
    }

    // Called when verticle is deployed
    @Override
    public void start() {
        log.info("start");
    }

    // Optional - called when verticle is undeployed
    @Override
    public void stop() {
        log.info("stop");
    }

}
